﻿using Sidvall.Security;

namespace Sidvall.Web.Security
{
	public class PrincipalContextProvider : IPrincipalContextProvider
	{
		#region Public Members

		#region SessionContext

		public System.Collections.Generic.IDictionary<string, object> SessionContext
		{
			get
			{
				System.Collections.Generic.Dictionary<string, object> value;

				if ((System.Web.HttpContext.Current == null) || (System.Web.HttpContext.Current.Session == null))
					return null;
				value = System.Web.HttpContext.Current.Session[_SessionContextName] as System.Collections.Generic.Dictionary<string, object>;
				if (value == null)
				{
					lock (_mutex)
					{
						value = System.Web.HttpContext.Current.Session[_SessionContextName] as System.Collections.Generic.Dictionary<string, object>;
						if (value == null)
						{
							value = new System.Collections.Generic.Dictionary<string, object>();
							System.Web.HttpContext.Current.Session[_SessionContextName] = value;
						}
					}

				}
				return value;
			}
		}

		#endregion

		#region ToString

		public override string ToString()
		{
			return "PrincipalContextProvider - System.Web.HttpContext.Current.Session";
		}

		#endregion

		#endregion
		#region Helper Members

		private const string _SessionContextName = "Sidvall.Context";
		private readonly object _mutex = new object();

		#endregion
		#region Constructors

		public PrincipalContextProvider()
		{
		}

		#endregion
	}
}
